package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @Transactional
    @Override
    public void updateStock(Long itemId, Integer num) {
        // Sql语句：update tb_item set stock = stock - #{num} where id = #{itemId}
        try {
            update().setSql("stock = stock -"+num).eq("id",itemId).update();
        } catch (Exception e) {
            throw new RuntimeException("库存不足，请及时联系商家上货！");
        }
    }
}
